// ws_micro_shop/pages/virtual-card/index.js
const api = require('../../utils/api.js');
const app = getApp();
let unlockTimer = null;

Page({
  data: {
    card_no: '',
    card_pwd: '',
    btn_txt: '确认充值',
    disabled: false,
    openEyes: false,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-19.png',
      text: '该功能已下架，暂不可用！',
    },
    isAuditMode: true,
    isShowAll: false
  },

  openEyes() {
    this.setData({
      openEyes: !this.data.openEyes,
    });
  },

  getCurStatus() {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.recharge.virtual_error,
      method: 'POST',
      success: function(res) {
        if (res.code === 0) {
          that.mechanism(res.addtime, res.wrong_count);
        } else if (res.code === 10001) {
          that.setData({
            isAuditMode: false
          })
        }
      },
      complete: function () {
        that.setData({
          isShowAll: true,
        })
      }
    });
  },

  mechanism(time, num) {
    let that = this;

    if (Number(num) === 3 || Number(num) === 4) {
      let nowTime = Math.round(new Date() / 1000);
      switch (num) {
        case 3:
          time += 300;
          break;
        case 4:
          time += 900;
          break;
      }
      let frozenTime = nowTime - time;
      if (frozenTime >= 0) {
        that.setData({
          disabled: false,
          btn_txt: '确认充值',
        });
      } else {
        // 冻结中
        let countdown = time - nowTime;
        unlockTimer = setInterval(function() {
          if (countdown > 0) {
            countdown--;
            that.setData({
              disabled: true,
            });
          } else {
            unlockTimer && clearInterval(unlockTimer);
            that.setData({
              disabled: false,
            });
          }
        }, 1000);
      }
    } else {
      that.setData({
        disabled: false,
        btn_txt: '确认充值',
      });
    }
  },

  onShow() {
    this.getCurStatus();
  },

  onHide() {
    unlockTimer && clearInterval(unlockTimer);
    if (wx.getStorageSync('unlockTime')) {
      wx.removeStorageSync('unlockTime');
    }
  },

  onUnload() {
    unlockTimer && clearInterval(unlockTimer);
    if (wx.getStorageSync('unlockTime')) {
      wx.removeStorageSync('unlockTime');
    }
  },

  cardNo(e) {
    this.setData({
      card_no: e.detail.value,
    });
  },

  cardPwd(e) {
    this.setData({
      card_pwd: e.detail.value,
    });
  },

  recharge() {
    let that = this;
    let { card_no, card_pwd } = that.data;
    if (card_no === '' || card_pwd === '') {
      wx.showModal({
        title: '提示',
        content: '请输入卡号和密码',
        showCancel: false,
      });
      return;
    }
    app.ws_micro_shoprequest({
      url: api.recharge.virtual_card,
      data: {
        card_no,
        card_pwd,
      },
      method: 'POST',
      success: function(res) {
        if (res.code === 0) {
          that.sendMsg(res.data.id);
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
            success: function() {
              app.util.navigateTo('ws_micro_shop/pages/virtual-card/list');
            },
          });
        } else {
          // 输入错误机制
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
            success: function() {
              if (
                Number(res.data.wrong_count) === 3 ||
                Number(res.data.wrong_count) === 4
              ) {
                that.mechanism(res.data.addtime, res.data.wrong_count);
              }
            },
          });
        }
      },
    });
  },

  sendMsg(id) {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.recharge.virtual_msg,
      data: {
        id,
        form_id: that.data.formId,
      },
      method: 'POST',
      success: function(res) {},
    });
  },

  collectFormId(e) {
    this.setData({
      formId: e.detail.formId,
    });
    app.collectFormId(e);
  },

  go() {
    app.util.navigateTo('ws_micro_shop/pages/virtual-card/list');
  },
});
